package ac.neec.timewatch;



/**
 * Created by G011C1102 on 14/01/18.
 */
public class StopWatch extends TimeWatchSubject {

    private TimeWatchInterface timerInterface;
    private long time;
    private boolean flag;

    public StopWatch(TimeWatchInterface timerInterface) {
        this.timerInterface = timerInterface;
    }

    private long getCurrentTime() {
        return System.currentTimeMillis();
    }

    public void cancel() {
        flag = false;
    }

    public void startTimer() {
        final long startTime = getCurrentTime();
        flag = true;
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag) {
                    time = (getCurrentTime() - startTime) ;
                    timerInterface.notifyTimerUpdata(time);
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
    }
}
